//
//  WGScanViewController.m
//  QuickCure1
//
//  Created by Apple on 16/11/7.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "WGScanViewController.h"

#define SCANVIEW_EdgeTop 40.0

#define SCANVIEW_EdgeLeft 50.0

#define TINTCOLOR_ALPHA 0.2 //浅色透明度

#define DARKCOLOR_ALPHA 0.5 //深色透明度

#define VIEW_WIDTH   [UIScreen mainScreen].bounds.size.width
#define VIEW_HEIGHT  [UIScreen mainScreen].bounds.size.height
#define getThemeColor colorWithRed:0.26 green:0.53 blue:0.19 alpha:1

@interface WGScanViewController () < ZBarReaderViewDelegate >


@end

@implementation WGScanViewController{
    
    /**中间线*/
    UIView *_QrCodeline;
    
    /**定时器*/
    NSTimer *_timer;
    
    
    //设置扫描画面
    UIView *_scanView;
    
    /**读取区域**/
    ZBarReaderView *_readerView;
    
}


#pragma
#pragma mark - 开启闪关灯
- (void)openLight {
    
    if ( _readerView . torchMode == 0 ) {
        
        _readerView . torchMode = 1 ;
        
    } else{
        
        _readerView . torchMode = 0 ;
  }
}




#pragma
#pragma mark - 创建定时器
- ( void )createTimer

{
    
    //创建一个时间计数
    
    _timer=[NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector (moveUpAndDownLine) userInfo: nil repeats: YES ];
    
}

#pragma
#pragma mark - 停止计时
- ( void )stopTimer

{
    
    if ([_timer isValid] == YES ) {
        
        [_timer invalidate];
        
        _timer = nil ;
        
    }
    
}


- (void)viewDidLoad {
    
    self . title = @"扫描二维码" ;
    
    //初始化扫描界面
    
    [ self setScanView ];
    
    
    _readerView = [[ ZBarReaderView alloc ] init ];
    
    
    
    _readerView . frame = CGRectMake ( 0 , 64 , VIEW_WIDTH , VIEW_HEIGHT - 64 );
    
    _readerView . tracksSymbols = NO ;
    
    _readerView . readerDelegate = self ;
    
    [ _readerView addSubview : _scanView ];
    
    
    
    //关闭闪光灯
    
    _readerView . torchMode = 0 ;
    
    
    [ self.view addSubview : _readerView ];
    
    [ _readerView start ];
    
    [ self createTimer ];
    
}


#pragma mark -- 实现代理方法   ZBarReaderViewDelegate

-( void )readerView:( ZBarReaderView *)readerView didReadSymbols:( ZBarSymbolSet *)symbols fromImage:( UIImage *)image 

{
    
    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol (symbols. zbarSymbolSet );
    
    NSString *symbolStr = [ NSString stringWithUTF8String : zbar_symbol_get_data (symbol)];
    
    
    //判断是否包含 头'http:'
    
    NSString *regex = @"http+:[^//s]*" ;
    
    NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"SELF MATCHES %@" ,regex];
    
    
    
    UIAlertView *alertView=[[ UIAlertView alloc ] initWithTitle : @"" message :symbolStr delegate : nil cancelButtonTitle : @"取消" otherButtonTitles : nil ];
    
    [alertView show ];
    
    
    //判断是否包含 头'ssid:'
    
    NSString *ssid = @"ssid+:[^//s]*" ;;
    
    NSPredicate *ssidPre = [ NSPredicate predicateWithFormat : @"SELF MATCHES %@" ,ssid];
    
    
    if ([predicate evaluateWithObject :symbolStr]) {
        
        
    }
    
    else if ([ssidPre evaluateWithObject :symbolStr]){
        
        
        NSArray *arr = [symbolStr componentsSeparatedByString : @";" ];
        
        
        NSArray * arrInfoHead = [[arr objectAtIndex : 0 ] componentsSeparatedByString : @":" ];
        
        
        NSArray * arrInfoFoot = [[arr objectAtIndex : 1 ] componentsSeparatedByString : @":" ];
        
        
        
        symbolStr = [ NSString stringWithFormat : @"ssid: %@ /n password:%@" ,
                     
                     [arrInfoHead objectAtIndex : 1 ],[arrInfoFoot objectAtIndex : 1 ]];
        
        
        UIPasteboard *pasteboard=[ UIPasteboard generalPasteboard ];
        
        //然后，可以使用如下代码来把一个字符串放置到剪贴板上：
        
        pasteboard. string = [arrInfoFoot objectAtIndex : 1 ];
        
    }
    
    
}


#pragma
#pragma mark - 布局界面
//二维码的扫描区域

- ( void )setScanView

{
    
    _scanView =[[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , VIEW_WIDTH , VIEW_HEIGHT - 64 )];
    
    _scanView . backgroundColor =[ UIColor clearColor ];
    
#pragma
#pragma mark -  最上部view 1.0
    
    
    UIView * upView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , VIEW_WIDTH , SCANVIEW_EdgeTop )];
    
    upView. alpha = TINTCOLOR_ALPHA ;
    
    upView. backgroundColor = [ UIColor blackColor ];
    
    [ _scanView addSubview :upView];
    
#pragma
#pragma mark -  最左边view 2.0
    
    //左侧的view
    
    UIView *leftView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    
    leftView. alpha = TINTCOLOR_ALPHA ;
    
    leftView. backgroundColor = [ UIColor blackColor ];
    
    [ _scanView addSubview :leftView];
    
#pragma
#pragma mark -  中间view 3.0
    /******************中间扫描区域****************************/
    
    UIImageView *scanCropView=[[ UIImageView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    
//    scanCropView.image=[UIImage imageNamed:@"二维码"];
    
    
    scanCropView. layer . borderColor =[ UIColor getThemeColor ]. CGColor ;
    
    scanCropView. layer . borderWidth = 2.0 ;
    
    
    scanCropView. backgroundColor =[ UIColor clearColor ];
    
    [ _scanView addSubview :scanCropView];
    
#pragma
#pragma mark -  最上部view 4.0
    
    //右侧的view
    
    UIView *rightView = [[ UIView alloc ] initWithFrame : CGRectMake ( VIEW_WIDTH - SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , SCANVIEW_EdgeLeft , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft )];
    
    rightView. alpha = TINTCOLOR_ALPHA ;
    
    rightView. backgroundColor = [ UIColor blackColor ];
    
    [ _scanView addSubview :rightView];
    
#pragma
#pragma mark -  底部view 5.0
    
    //底部view
    
    UIView *downView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop , VIEW_WIDTH , VIEW_HEIGHT -( VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft + SCANVIEW_EdgeTop )- 64 )];
    
    //downView.alpha = TINTCOLOR_ALPHA;
    
    downView. backgroundColor = [[ UIColor blackColor] colorWithAlphaComponent : TINTCOLOR_ALPHA ];
    
    [ _scanView addSubview :downView];
    
    
#pragma
#pragma mark -  将二维码对准方框，即可自动扫描 label
    //用于说明的label
    
    UILabel *labIntroudction= [[ UILabel alloc ] init ];
    
    labIntroudction. backgroundColor = [ UIColor clearColor ];
    
    labIntroudction. frame = CGRectMake ( 0 , 5 , VIEW_WIDTH , 20 );
    
    labIntroudction. numberOfLines = 1 ;
    
    labIntroudction. font =[ UIFont systemFontOfSize : 17.0 ];
    
    labIntroudction. textAlignment = NSTextAlignmentCenter ;
    
    labIntroudction. textColor =[ UIColor whiteColor ];
    
    labIntroudction. text = @"将二维码对准方框，即可自动扫描" ;
    
    [downView addSubview :labIntroudction];
    
    
    UIView *darkView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , downView. frame . size . height - 100.0 , VIEW_WIDTH , 100.0 )];
    
    darkView. backgroundColor = [[ UIColor blackColor ]  colorWithAlphaComponent : DARKCOLOR_ALPHA ];
    
    [downView addSubview :darkView];
    
#pragma
#pragma mark - 用于开关灯操作的button
    
    
    
    UIButton *openButton=[[ UIButton alloc ] initWithFrame : CGRectMake ( 50 , 20 , 300.0 , 40.0 )];
    
    [openButton setTitle : @"开启闪光灯" forState: UIControlStateNormal ];
    
    [openButton setTitleColor :[ UIColor whiteColor ] forState : UIControlStateNormal ];
    
    openButton. titleLabel . textAlignment = NSTextAlignmentCenter ;
    
    openButton. backgroundColor =[ UIColor getThemeColor ];
    
    openButton. titleLabel . font =[ UIFont systemFontOfSize : 22.0 ];
    
    [openButton addTarget : self action : @selector (openLight) forControlEvents : UIControlEventTouchUpInside ];
    
    [darkView addSubview :openButton];
    
    
#pragma
#pragma mark -  _QrCodeline
    //画中间的基准线
    
    _QrCodeline = [[ UIView alloc ] initWithFrame : CGRectMake ( SCANVIEW_EdgeLeft , SCANVIEW_EdgeTop , VIEW_WIDTH - 2 * SCANVIEW_EdgeLeft , 10 )];
    
    _QrCodeline . backgroundColor = [ UIColor getThemeColor ];
    
    [ _scanView addSubview : _QrCodeline ];
    
}



#pragma
#pragma mark - 视图将要出现
- ( void )viewWillDisappear:( BOOL )animated

{
    
    [ super viewWillDisappear :animated];
    
    
    if ( _readerView . torchMode == 1 ) {
        
        _readerView . torchMode = 0 ;
        
    }
    
    [ self stopTimer ];
    
    
    [ _readerView stop ];
    
    
}


#pragma
#pragma mark - 设置二维码的扫描线
//二维码的横线移动

- ( void )moveUpAndDownLine

{
    
    CGFloat Y= _QrCodeline . frame . origin . y ;
    
    //CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH-2*SCANVIEW_EdgeLeft, 1)]
    
    if (VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop==Y){
        
        
        [UIView beginAnimations: @"asa" context: nil ];
        
        [UIView setAnimationDuration: 1 ];
        
        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, SCANVIEW_EdgeTop, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft, 2 );
        
        [UIView commitAnimations];
        
    } else if (SCANVIEW_EdgeTop==Y){
        
        [UIView beginAnimations: @"asa" context: nil ];
        
        [UIView setAnimationDuration: 1 ];
        
        _QrCodeline.frame=CGRectMake(SCANVIEW_EdgeLeft, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft+SCANVIEW_EdgeTop, VIEW_WIDTH- 2 *SCANVIEW_EdgeLeft, 2);
        
        [UIView commitAnimations];
        
    }
    
}




@end
